﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Sw;
using FytSoa.Extensions;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FytSoa.Api.Controllers.Shop
{
    [Route("api/shop/[controller]")]
    [ApiController]
    [JwtAuthorize(Roles = "Admin")]
    public class GoodsController : ControllerBase
    {
        private readonly ICmsColumnService _classService;
        private readonly ISw_brandService _brandService;
        private readonly ISw_typesService _typeService;
        private readonly ISw_storeService _storeService;
        private readonly ISw_goodsService _goodsService;
        private readonly ISw_spectService _spectService;
        private readonly ISw_spectcodeService _spectcodeService;
        private readonly IMember_GroupService _groupService;
        public GoodsController(ISw_brandService brandService
            , ISw_typesService typeService
            , ICmsColumnService classService
            , ISw_storeService storeService
            , ISw_goodsService goodsService
            , IMember_GroupService groupService
            , ISw_spectService spectService
            , ISw_spectcodeService spectcodeService)
        {
            _brandService = brandService;
            _typeService = typeService;
            _classService = classService;
            _storeService = storeService;
            _goodsService = goodsService;
            _groupService = groupService;
            _spectService = spectService;
            _spectcodeService = spectcodeService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<IActionResult> Add([FromQuery]PageParm param)
        {
            return Ok(await _goodsService.GetPageGoods(param));
        }

        /// <summary>
        /// 根据id获得信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("getmodel")]
        public async Task<IActionResult> GetModel([FromBody]ParmString obj)
        {
            //查询实体
            var goodsModel = await _goodsService.GetModelAsync(m => m.Guid == obj.parm);
            //查询会员组-会员组折扣
            var groupList = await _groupService.GetListAsync(m=>m.Types==2,m=>m.Level,DbOrderEnum.Asc);
            //判断是否添加
            if (string.IsNullOrEmpty(goodsModel.data.Guid))
            {
                //查询全局设置的分销
                goodsModel.data.FenXiaoJson = new List<Core.Model.Sw.SwGoodsRetailJson>
                {
                    new Core.Model.Sw.SwGoodsRetailJson()
                    {
                        level = 1,
                        name = "一级分销"
                    },
                    new Core.Model.Sw.SwGoodsRetailJson()
                    {
                        level = 2,
                        name = "二级分销"
                    },
                    new Core.Model.Sw.SwGoodsRetailJson()
                    {
                        level = 3,
                        name = "三级分销"
                    }
                };
                var disCountList = new List<Core.Model.Sw.SwGoodsDisCountJson>();
                if (groupList.data!=null && groupList.data.Count>0)
                {
                    foreach (var item in groupList.data)
                    {
                        disCountList.Add(new Core.Model.Sw.SwGoodsDisCountJson() {
                            guid=item.Guid,
                            name=item.Name,
                            ratio=item.DisCount
                        });
                    }
                }
                goodsModel.data.DisCountJson = disCountList;
                goodsModel.data.Number = Utils.Number(6);
            }
            //查询分类
            var siteGuid = SiteTool.CurrentSite?.Guid;
            var dblist = await _classService.GetListAsync(m => m.SiteGuid == siteGuid && m.TypeID == 2, m => m.Sort, DbOrderEnum.Asc);
            var list = _classService.RecursiveModule(dblist.data);
            foreach (var item in list)
            {
                item.Title = Utils.LevelName(item.Title, item.ClassLayer);
            }
            //查询品牌
            var gx = await _brandService.GetListAsync();
            //查询商家
            var storeList =await _storeService.GetListAsync();
            //查询规格
            var spectList =await _spectService.GetListAsync(m=>true,m=>m.Sort,DbOrderEnum.Desc);
            //查询规格值
            var spectCodeList =await _spectcodeService.GetListAsync();
            var resModel = new SwGoodsDto()
            {
                model = goodsModel.data,
                column = list,
                brand = gx.data,
                store = storeList.data,
                spect = spectList.data,
                spectCode = spectCodeList.data
            };
            return Ok(new ApiResult<SwGoodsDto>() {
                data=resModel
            });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<IActionResult> Add([FromBody]Sw_goods model)
        {
            model.Guid = Guid.NewGuid().ToString();
            return Ok(await _goodsService.AddAsync(model));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<IActionResult> Update([FromBody]Sw_goods model)
        {
            return Ok(await _goodsService.UpdateAsync(model));
        }

        /// <summary>
        /// 上下架操作
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("audit")]
        public async Task<IActionResult> UpdateAudit([FromBody]PageParm param)
        {
            return Ok(await _goodsService.UpdateAuditOption(param));
        }

        /// <summary>
        /// 删除  更改状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Delete([FromBody]ParmString obj)
        {
            var list = Utils.StrToListString(obj.parm);
            //查询在更新
            var dataList =await _goodsService.GetListAsync(m=>list.Contains(m.Guid));
            foreach (var item in dataList.data)
            {
                item.IsDel = true;
            }
            return Ok(await _goodsService.UpdateAsync(dataList.data));
        }
    }
}